using MicroMsg.Common.Event;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Scene;
using MicroMsg.Storage;
using MicroMsg.UI.ImageFilter;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class PersonalInfoView : BasePage
	{
		private const int MAX_NAME_LEN = 32;
		private EventWatcher m_HeadImageSyncOK;
		private ApplicationBar mBar;
		private Stream chosenPhoto;
		private string mStrNameOrigin = "";
		private bool mBHasBrandListCtrl;
		private ProfileBrandListCtrl brandListCtrl;
		internal Grid LayoutRoot;
		internal Grid ContentPanel;
		internal Image imgHead;
		internal Button btnChangeHeadImg;
		internal PhoneTextBox txtInputNickName;
		internal SetAccInfoSexAreaSigCtrl ctrlSetSexAreaSig;
		internal Grid BrandLayout;
		private bool _contentLoaded;
		public PersonalInfoView()
		{
			this.InitializeComponent();
			base.set_ApplicationBar(this.GetApplicationBar());
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null && object_0.Length != 0)
			{
				string text = object_0[0] as string;
				if ("SetHeadHint" == text)
				{
					this.LayoutRoot.set_Visibility(1);
					this.btnChangeHeadImg_Click(null, null);
				}
			}
			this.initData();
		}
		protected override void OnPageOut()
		{
			this.doFinish();
			ProfileBrandListCtrl.PushOneItem(this.brandListCtrl);
		}
		private void doFinish()
		{
			ServiceCenter.sceneNewSync.doScene(1, syncScene.MM_NEWSYNC_SCENE_OTHER);
			if (this.m_HeadImageSyncOK == null)
			{
				return;
			}
			EventCenter.removeEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.m_HeadImageSyncOK);
			this.m_HeadImageSyncOK = null;
		}
		private void initData()
		{
			this.m_HeadImageSyncOK = new EventWatcher(this, null, new EventHandlerDelegate(this.onHeadImageSyncOK));
			EventCenter.registerEventWatcher(EventConst.ON_MGR_HEAD_IMG_READY, this.m_HeadImageSyncOK);
			this.refreshView();
		}
		private void refreshView()
		{
			this.txtInputNickName.set_Text(this.trimToEmpty(ToolFunction.SubStringDisLen(AccountMgr.strNickName, 32)));
			BitmapImage headimg = AccountMgr.getHeadimg();
			if (headimg != null)
			{
				this.imgHead.set_Source(headimg);
			}
			this.btnChangeHeadImg.set_IsEnabled(true);
			this.disBrandList();
		}
		protected override void OnPageBack()
		{
			if (this.LayoutRoot.get_Visibility() == 1)
			{
				this.LayoutRoot.set_Visibility(0);
			}
			this.refreshView();
		}
		private void onHeadImageSyncOK(EventWatcher watcher, BaseEventArgs evtArgs)
		{
			List<object> list = evtArgs.mObject as List<object>;
			if (list != null || list.Contains(AccountMgr.curUserName))
			{
				BitmapImage headimg = AccountMgr.getHeadimg();
				if (headimg != null)
				{
					this.imgHead.set_Source(headimg);
				}
			}
		}
		private string trimToEmpty(string string_0)
		{
			if (string_0 != null)
			{
				return string_0.Trim();
			}
			return "";
		}
		public ApplicationBar GetApplicationBar()
		{
			return null;
		}
		private void cancelButton_Click(object sender, EventArgs e)
		{
			PhoneApplicationFrame phoneApplicationFrame = Application.get_Current().get_RootVisual() as PhoneApplicationFrame;
			BasePage basePage = phoneApplicationFrame.get_Content() as BasePage;
			if (basePage != null)
			{
				basePage.get_NavigationService().GoBack();
			}
		}
		private void layoutQCode_Click(object sender, EventArgs e)
		{
			ToolFunction.GotoPageForUserControl("/Source/Plugin/QRCode/UI/UserQRCodePage.xaml", null, false);
		}
		private void btnChangeHeadImg_Click(object sender, RoutedEventArgs e)
		{
			base.Focus();
			this.btnChangeHeadImg.set_IsEnabled(false);
			base.GoToPage("/Source/UI/ImageFilter/ImageFilterPage.xaml", new object[]
			{
				new ImageFilterConfig(new ImageFilterCallBack(this.onImageFilterCallBack), ImageFilterGetPicMode.GetPicMode_Choose_With_Take, 480, 480, null)
				{
					isSaveToAlbum = true,
					isNeedFilter = true
				}
			}, false);
		}
		private void onImageFilterCallBack(Stream resultData)
		{
			this.chosenPhoto = resultData;
			this.showProgressCover(strings.Set_Personal_SendingImage);
			base.get_Dispatcher().BeginInvoke(delegate
			{
				HDheadImgMgr.uploadHDheadimg(resultData, new onUploadHDheadImgCallback(this.onUploadHDheadImgCallback));
				this.btnChangeHeadImg.set_IsEnabled(true);
			});
		}
		public void onUploadHDheadImgCallback(bool bSuccess)
		{
			this.hideProgressCover();
			if (bSuccess)
			{
				AccountMgr.saveHeadImg(this.chosenPhoto);
				MessageBoxManager.ShowToast(strings.Set_Personal_SendImage_OK, 2500, null, false);
				return;
			}
			MMMessageBox.Show(strings.Set_Personal_SendImage_Err, true, "确定");
		}
		private void showHDImage(object sender, RoutedEventArgs e)
		{
			base.GoToPage("/Source/UI/Setting/HighQualityHeadImgPage.xaml", new object[]
			{
				this.imgHead.get_Source(),
				AccountMgr.curUserName,
				142.0
			}, false);
		}
		private void onNameChanged(object sender, TextChangedEventArgs e)
		{
			if (sender != null && sender is PhoneTextBox)
			{
				PhoneTextBox phoneTextBox = sender as PhoneTextBox;
				if (ToolFunction.getStrDisLen(phoneTextBox.get_Text()) <= 32)
				{
					this.mStrNameOrigin = phoneTextBox.get_Text();
					return;
				}
				if (this.mStrNameOrigin != null && this.mStrNameOrigin.get_Length() < phoneTextBox.get_Text().get_Length())
				{
					phoneTextBox.set_Text(this.mStrNameOrigin);
					phoneTextBox.set_SelectionStart(phoneTextBox.get_Text().get_Length());
				}
				this.mStrNameOrigin = phoneTextBox.get_Text();
			}
		}
		private void txtGotFofus(object sender, RoutedEventArgs e)
		{
			PhoneTextBox phoneTextBox = sender as PhoneTextBox;
			phoneTextBox.set_BorderBrush(R.theme.COMMON_BRUSH);
		}
		private void txtLostFofus(object sender, RoutedEventArgs e)
		{
			PhoneTextBox phoneTextBox = sender as PhoneTextBox;
			phoneTextBox.set_BorderBrush(null);
			if (phoneTextBox == this.txtInputNickName)
			{
				string text = this.trimToEmpty(this.txtInputNickName.get_Text());
				if (text.get_Length() == 0)
				{
					MMMessageBox.Show(strings.Set_Personal_NameEmptyMsg, true, "确定");
					this.txtInputNickName.set_Text(AccountMgr.strNickName);
					return;
				}
				if (ToolFunction.getStrDisLen(text) > 32)
				{
					MMMessageBox mMMessageBox = MMMessageBox.Show(strings.Set_Personal_NameTooLong, false, "确定");
					mMMessageBox.OkBtn.add_Click(new RoutedEventHandler(this.dialog_OK_Btn_Click));
					return;
				}
				AccountMgr.strNickName = this.txtInputNickName.get_Text();
				this.txtInputNickName.set_Foreground(R.theme.COMMON_INPUTBOX_CONTENT_WORD_BRUSH);
				Contact contact = StorageMgr.contact.method_0(AccountMgr.curUserName);
				if (contact != null)
				{
					contact.strNickName = this.txtInputNickName.get_Text();
					StorageMgr.contact.modify(contact);
				}
			}
		}
		private void dialog_OK_Btn_Click(object sender, RoutedEventArgs e)
		{
			this.txtInputNickName.Focus();
		}
		private void inputTxt_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.get_Key() == 3)
			{
				base.Focus();
			}
		}
		private void showProgressCover(string strCoverTitle)
		{
			ProcessingCover.getProgressBar(this.LayoutRoot, strCoverTitle, (SolidColorBrush)Application.get_Current().get_Resources().get_Item("HalfOpacityBrush1"), false, false, null);
		}
		private void hideProgressCover()
		{
			ProcessingCover.releaseProgressBar(this.LayoutRoot);
		}
		private void imgQCode_Tap(object sender, GestureEventArgs e)
		{
		}
		private void disBrandList()
		{
			if (this.mBHasBrandListCtrl)
			{
				return;
			}
			Contact contact = StorageMgr.contact.method_0(AccountMgr.curUserName);
			if (contact != null && !string.IsNullOrEmpty(contact.strMyBrandList))
			{
				if (this.BrandLayout.get_Children().get_Count() > 0)
				{
					this.BrandLayout.get_Children().Clear();
				}
				this.brandListCtrl = ProfileBrandListCtrl.GetOneItem();
				this.brandListCtrl.mBShowNotShowCnt = true;
				this.brandListCtrl.txtAreaTitle.set_FontSize(20.0);
				this.brandListCtrl.txtAreaTitle.set_Foreground(R.theme.COMMON_INPUTBOX_TITLE_WORD_BRUSH);
				this.BrandLayout.get_Children().Add(this.brandListCtrl);
				this.mBHasBrandListCtrl = true;
				this.brandListCtrl.ItemObject = contact;
			}
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Setting/PersonalInfoView.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.ContentPanel = (Grid)base.FindName("ContentPanel");
			this.imgHead = (Image)base.FindName("imgHead");
			this.btnChangeHeadImg = (Button)base.FindName("btnChangeHeadImg");
			this.txtInputNickName = (PhoneTextBox)base.FindName("txtInputNickName");
			this.ctrlSetSexAreaSig = (SetAccInfoSexAreaSigCtrl)base.FindName("ctrlSetSexAreaSig");
			this.BrandLayout = (Grid)base.FindName("BrandLayout");
		}
	}
}
